﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RouterControl.IO;
using RouterControl.IO.Services;
using RouterControl.IO.UPnP;
using RouterControl.Whs.Wizards;

namespace RouterControl.Diag
{
    public partial class PageFeatureTest : WizardPageClientBannerTop
    {
        private readonly log4net.ILog _log = log4net.LogManager.GetLogger(typeof(PageFeatureTest));
        private IUPnPDevice _selectedDevice;
        private CommonInternetGateway _selectedRouter;

        private WizardMain ParentWizardMain
        {
            get { return (WizardMain)this.ParentWizard; }
        }

        public PageFeatureTest()
        {
            InitializeComponent();
        }

        private void PageFeatureTest_BackButtonClick(object sender, EventArgs e)
        {
            this.SetCurrentPage<PageSelectRouter>();
        }

        private void PageFeatureTest_OkButtonClick(object sender, EventArgs e)
        {
            this.ParentWizard.Close();
        }

        private void diagList_OnDiagnosisDone(object sender, DiagList.DiagnosisDoneEventArgs e)
        {
            this.BackButtonEnabled = true;
            //this.CancelButtonEnabled = true;
            this.OkButtonEnabled = true;

            this.labelResult.Text = string.Format(
                (e.Result != DiagList.DiagResult.Successful) ?
                Properties.Resources.txtFeatureTestFailed :
                Properties.Resources.txtFeatureTestAccomplished,
                this.ParentWizardMain.SelectedDevice.FriendlyName
                );
        }

        public override void StartRealWork()
        {
            base.StartRealWork();

            // prepare
            _selectedDevice = this.ParentWizardMain.SelectedDevice;

            // create device
            try
            {
                var router = new RootDevice(this.ParentWizardMain.SelectedDevice);

                _selectedRouter = new CommonInternetGateway(router);
            }
            catch (Exception x)
            {
                _log.Error(string.Format(
                    "Failed to create managed UPNP device: {0} ({1})",
                    _selectedDevice.FriendlyName,
                    _selectedDevice.UniqueDeviceName
                    ), x);

                this.ParentWizardMain.Errors.Add(new WizardMain.ErrorInfo()
                {
                    Severity = WizardMain.Severity.Error,
                    Title = Properties.Resources.txtManagedFailedTitle,
                    Text = string.Format(Properties.Resources.txtManagedFailedText, _selectedDevice.FriendlyName),
                    Error = x
                });

                this.SetCurrentPage<PageShowErrors>();
            }

            // add diagnosis
            this.diagList.ClearAllDiagnosises();
            this.diagList.AddDiagnosis(new FeatureTestConnectionStatus(_selectedRouter));
            this.diagList.AddDiagnosis(new FeatureTestConnectionSpeed(_selectedRouter));
            this.diagList.AddDiagnosis(new FeatureTestUptime(_selectedRouter));
            this.diagList.AddDiagnosis(new FeatureTestTraffic(_selectedRouter));
            this.diagList.AddDiagnosis(new FeatureTestHijackingDetection(_selectedRouter));

            // run diagnosis
            this.diagList.RunDiagnosisAsync();
        }
    }
}
